﻿// --------------------------------------------------------------------------------------
// <copyright file="HyphenatedRoute.cs" company="NGroup">
// Copyright (c) NGroup. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------
// <history>
//     <change who="Bui Thanh Nam" date="2013.12.04">Create</change>
// </history>
// --------------------------------------------------------------------------------------

namespace NGroup.VTL.Core.Routing
{
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    public class HyphenatedRoute : Route
    {
        public HyphenatedRoute(string url, object defaults) :
            base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
        { }

        public HyphenatedRoute(string url, object defaults, object constraints) :
            base(url, new RouteValueDictionary(defaults), new RouteValueDictionary(constraints), new MvcRouteHandler())
        { }

        /// <summary>
        /// Returns information about the requested route.
        /// </summary>
        /// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
        /// <returns>An object that contains the values from the route definition.</returns>
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var routeData = base.GetRouteData(httpContext);

            if (routeData != null)
            {
                RouteValueDictionary values = routeData.Values;

                values["controller"] = (values["controller"] as string)
                                       .Replace("-", "_");

                values["action"] = (values["action"] as string)
                                   .Replace("-", "_");
            }

            return routeData;
        }

        /// <summary>
        /// Gets the virtual path.
        /// </summary>
        /// <param name="ctx">The CTX.</param>
        /// <param name="values">The values.</param>
        /// <returns>VirtualPathData.</returns>
        public override VirtualPathData GetVirtualPath(RequestContext ctx, RouteValueDictionary values)
        {
            values["controller"] = (values["controller"] as string)
                                   .Replace("_", "-").ToLower();

            values["action"] = (values["action"] as string)
                               .Replace("_", "-").ToLower();

            return base.GetVirtualPath(ctx, values);
        }
    }
}
